package org2.jogo.models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org2.jogo.R;
import adapters.LeaderboardDbAdapter;
import android.database.Cursor;

public class Leaderboard {

    private static final String TABLE_NAME = "leaderboard";
    private static Leaderboard instance;

    private Leaderboard() {
    }

    public static Leaderboard getInstance() {
        if (instance == null)
            instance = new Leaderboard();
        return instance;
    }

    public List<Pontuacao> getTopFive(LeaderboardDbAdapter adapter, int dificuldade) {
        Cursor c = adapter.getBestPontuacoes(dificuldade);
        c.moveToFirst();
        List<Pontuacao> pontuacoes = new ArrayList<Pontuacao>();
        int count = 0;
        while (!c.isAfterLast() && count < 5) {
            pontuacoes.add(Pontuacao.getFromCursor(c));
            c.moveToNext();
            count++;
        }
        c.close();
        return pontuacoes;
    }
    
    public List<HashMap<String, String>> getHashMapTopFive(List<Pontuacao> pontuacoes) {
        List<HashMap<String, String>> info = new ArrayList<HashMap<String,String>>();
        int i = 1;
        for (Pontuacao pontuacao : pontuacoes) {
            info.add(pontuacao.getHashmapInfo(i++));
        }
        return info;
    }
    
    public String[] getColumnNames() {
        return new String[] {"nome", "pontuacao", "data" };
    }
    
    public int[] getColumnIds() {
        return new int[] { R.id.txtLeaderNome, R.id.txtLeaderPontuacao, R.id.txtLeaderData };
    }

    public static String getTableName() {
        return TABLE_NAME;
    }

    public static String getTableCreationSQL() {
        return "create table " + TABLE_NAME + " (_id integer primary key autoincrement,"
                + "nome text not null, dificuldade integer, pontuacao float, data text);";
    }
}
